package net.lwf.exception;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class ExceptionId {

	public static String getExceptionID() {
		String exceptionId = null;
		try {
			InetAddress address = InetAddress.getLocalHost();
			//exceptionId = InetAddress.getLocalHost().getHostName();
			exceptionId = address.getHostName() + ":" + address.getHostAddress();
		} catch (UnknownHostException ue) {
			exceptionId = ".....";
		}
		String uniqueJVMID = System.getProperty("logging.jvm.unique.id",
				"jvm999");
		exceptionId = exceptionId + ":" + uniqueJVMID + ":"
				+ System.currentTimeMillis();
		
		return exceptionId;
	}

	public static String getExceptionID(String msg) {
		String exceptionId = getExceptionID();
		String existingUId = searchMsg(msg, exceptionId);
		if (existingUId != null)
			exceptionId = existingUId;
		return exceptionId;
	}

	private static String searchMsg(String msg, String exceptionId) {
		String existingId = null;
		if (msg != null) {
			int msgIndex = msg.indexOf("uniqueLogId");
			if (msgIndex != -1) {
				int startingPoint = msgIndex + 1 + "uniqueLogId".length();
				existingId = msg.substring(startingPoint, startingPoint
						+ exceptionId.length());
			}
		}
		return existingId;
	}

	public static final String UNIQUE_LOGID_NAME = "uniqueLogId";

}
